<?php
// +----------------------------------------------------------------------
// |用户管理控制器
// +----------------------------------------------------------------------


namespace app\admin\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Cache;
use think\facade\Config;
use think\Request;
use app\admin\Model\AuthGroupsModel;
use app\common\Model\UserModel;
use app\common\system\UserService;
use app\common\Model\StroageModel;



class User extends BaseController
{
    //用户列表
    public function index(){
         //仓库的信息
         $field = 'id,name';//要显示的字段
         $where[]=['status','=',1];//查询条件仓        
         $storagelist = $this->infos(new StroageModel(),$field,$where);
         $storagelist=array_column($storagelist,null,'id');
       
        if(request()->isPost()){
            $param = request()->param();          
            $result = UserService::list($param);        
            if($result['list']){             
              foreach($result['list'] as $k=>$v){              ;
                $result['list'][$k]['storage_name']=isset($storagelist[$v['storgeid']])?$storagelist[$v['storgeid']]['name']:'';
              }
            }
            success($result);
        }else{  
             $field = 'id,nickname';//要显示的字段
             $where=[];//查询条件
             $userinfo =$this->infos(new UserModel(),$field,$where);
             //查询权限组
             $field = 'id,title';//要显示的字段
             $where=[];//查询条件
             $authgroupsinfo = $this->infos(new AuthGroupsModel(),$field,$where);          
         
            View::assign('authgroupsinfo', json_encode($authgroupsinfo));
            View::assign('userinfo', json_encode($userinfo));
            View::assign('storage', json_encode($storagelist));
            return View::fetch();
        }    
    }

      //用户操作删除
      public function delete(){
        $param = request()->param();
        $UserService=new UserService();  
        $result = $UserService->delete($param);
        success($result);
      }
    //用户权限组添加跟账户添加与及修改列表
      public function  purview(){
        if(request()->isPost()){
          $param = request()->post();
          $data= UserService::purview($param);         
          success($data);
        }else{
            $param = request()->get();              
            View::assign('id',$param['id']); 
            //查询权限组
            $field = 'id,title';//要显示的字段
            $where[]=['status','=',1];//查询条件      
            $authgroupsinfo = $this->infos(new AuthGroupsModel(),$field,$where);           
            View::assign('authGroup', json_encode($authgroupsinfo));   
            //查询仓库
            $storagelist=   (new StroageModel())->getActiveStatus();  //获取仓库 
            View::assign('storagelist', json_encode($storagelist));       
            return View::fetch();
        }
               
      
      }

        /**
     * 权限添加
     * @param array $param 前台传过来的参数  
     * @return array
     */   
      public function  create_purview(){
        if(request()->isPost()){
          $param = request()->post();         
          $data= UserService::create_purview($param); 
          success($data);
        }
    }

      /**
     * 获取权限组
     * @param array $param 前台传过来的参数  
     * @return array
     */   
      public function  get_group_user_list(){
        if(request()->isPost()){
          $param = request()->post();         
          $data= UserService::get_group_user_list($param); 
          success($data);
        }
    }
}